package com.py.credit.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.SysUser;
import com.py.credit.service.LhjcHongHeimdService;
import com.py.credit.service.RemoteBaseService;
import com.py.credit.service.impl.LhjcUploadAjbhShedule;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.HttpServletHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 联合奖惩红黑名单事件发起
 */
@RestController
@RequestMapping("/jcmd")
public class LhjcHongHeimdController {

    @Autowired
    private LhjcHongHeimdService lhjcHongHeimdService;

    @Autowired
    private RemoteBaseService remoteBaseService;

    @Autowired
    private LhjcUploadAjbhShedule lhjcUploadAjbhShedule;

    /**
     * 红黑名单列表
     *
     * @param jclb    奖惩类别：C:惩戒 J:激励
     * @param type    主体类型：C：法人 P:自然人
     * @param ztmc    企业名称
     * @param bm      身份证编码
     * @param flag    奖惩状态：A:未处理 B:比对失败 C:入库失败 D:处理成功
     * @param page    分页条件
     * @param request
     * @return
     */
    @RequestMapping(value = "/list.json")
    public RestServiceResult getListForPage(
            @RequestParam(value = "jclb", defaultValue = "", required = true) String jclb,
            @RequestParam(value = "type", defaultValue = "", required = true) String type,
            @RequestParam(value = "ztmc", defaultValue = "", required = true) String ztmc,
            @RequestParam(value = "bm", defaultValue = "", required = true) String bm,
            @RequestParam(value = "flag", defaultValue = "", required = true) String flag, Page page,
            HttpServletRequest request) {
        RestServiceResult restServiceResult = new RestServiceResult();
        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;

        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null != userInfo) {

            } else {
                ExceptionLogger.error("LhjcHongHeimdController#getListForPage", "未获取到当前用户信息！");
                restServiceResult.setCode(RestServiceResult.CODE_UNKNOW);
                restServiceResult.setMsg("未获取到当前用户信息！");
                return restServiceResult;
            }
        } else {
            ExceptionLogger.error("LhjcHongHeimdController#getListForPage", "未获取到当前用户信息！");
            restServiceResult.setCode(RestServiceResult.CODE_UNKNOW);
            restServiceResult.setMsg("未获取到当前用户信息！");
            return restServiceResult;
        }

        String gzNames = lhjcHongHeimdService.getGzNames(userInfo.getOfficeId() + "", jclb,
                HttpServletHelper.getUserToken(request));
        List<LinkedHashMap> map = new ArrayList<LinkedHashMap>();
        if (gzNames == null || gzNames.trim().equals("")) {
            restServiceResult.setCount(0);
            restServiceResult.setData(map);
        } else {
            map = lhjcHongHeimdService.selectHongheiMdForPage(jclb, type, ztmc, bm, flag,
                    Arrays.asList(gzNames.split(",")), page);
            restServiceResult.setCount(page.getTotalCount());
            restServiceResult.setData(map);
        }

        return restServiceResult;
    }

    /**
     * 红黑名单奖惩事件发起
     *
     * @param jclb      奖惩类别：C:惩戒 J:激励
     * @param type      主体类型：C：法人 P:自然人
     * @param recordIds 主键id(多个逗号拼接)
     * @param request
     * @return
     */
    @RequestMapping(value = "/add.json")
    public RestServiceResult add(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb,
                                 @RequestParam(value = "type", defaultValue = "", required = true) String type,
                                 @RequestParam(value = "recordIds", defaultValue = "", required = true) String recordIds,
                                 HttpServletRequest request) {
        RestServiceResult restServiceResult = new RestServiceResult();
        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;

        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null != userInfo) {

            } else {
                ExceptionLogger.error("LhjcHongHeimdController#add", "未获取到当前用户信息！");
                restServiceResult.setCode(RestServiceResult.CODE_UNKNOW);
                restServiceResult.setMsg("未获取到当前用户信息！");
                return restServiceResult;
            }
        } else {
            ExceptionLogger.error("LhjcHongHeimdController#add", "未获取到当前用户信息！");
            restServiceResult.setCode(RestServiceResult.CODE_UNKNOW);
            restServiceResult.setMsg("未获取到当前用户信息！");
            return restServiceResult;
        }

        String[] recordIdArr = recordIds.split(",");
        if (recordIdArr == null || recordIdArr.length == 0) {
            restServiceResult.setCode(RestServiceResult.CODE_UNKNOW);
            restServiceResult.setMsg("至少选择一条数据发起");
            return restServiceResult;
        }
        
        LinkedHashMap<?, ?> lists = lhjcHongHeimdService.add(jclb, type, Arrays.asList(recordIdArr), HttpServletHelper.getUserToken(request), "N", userInfo);

        restServiceResult.setData(lists);
        return restServiceResult;
    }

    @RequestMapping(value = "/addGlcfZrr.json")
    public RestServiceResult addGlcfZrr(
            @RequestParam(defaultValue = "", required = true, value = "glcfList") String glcfList,
            HttpServletRequest request) {
        RestServiceResult restServiceResult = new RestServiceResult();
        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;

        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null != userInfo) {

            } else {
                ExceptionLogger.error("LhjcHongHeimdController#addGlcfZrr", "未获取到当前用户信息！");
                restServiceResult.setCode(RestServiceResult.CODE_UNKNOW);
                restServiceResult.setMsg("未获取到当前用户信息！");
                return restServiceResult;
            }
        } else {
            ExceptionLogger.error("LhjcHongHeimdController#addGlcfZrr", "未获取到当前用户信息！");
            restServiceResult.setCode(RestServiceResult.CODE_UNKNOW);
            restServiceResult.setMsg("未获取到当前用户信息！");
            return restServiceResult;
        }

        // json解析字符串
        JSONArray zrrGlcfArray = JSON.parseArray(glcfList);
        restServiceResult
                .setData(lhjcHongHeimdService.addGlcfZrr(zrrGlcfArray, HttpServletHelper.getUserToken(request)));
        return restServiceResult;
    }

    /**
     * 红黑名单奖惩事件发起比对校验
     *
     * @param jclb     奖惩类别：C:惩戒 J:激励
     * @param type     主体类型：C：法人 P:自然人
     * @param recordId 主键id
     * @param request
     * @return
     */
    @RequestMapping(value = "/compareCheck.json")
    public RestServiceResult compareCheck(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb,
                                          @RequestParam(value = "type", defaultValue = "", required = true) String type,
                                          @RequestParam(value = "recordId", defaultValue = "", required = true) String recordId,
                                          HttpServletRequest request) {
        RestServiceResult restServiceResult = new RestServiceResult();
        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;

        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null != userInfo) {

            } else {
                ExceptionLogger.error("LhjcHongHeimdController#compareCheck", "未获取到当前用户信息！");
                restServiceResult.setCode(RestServiceResult.CODE_UNKNOW);
                restServiceResult.setMsg("未获取到当前用户信息！");
                return restServiceResult;
            }
        } else {
            ExceptionLogger.error("LhjcHongHeimdController#compareCheck", "未获取到当前用户信息！");
            restServiceResult.setCode(RestServiceResult.CODE_UNKNOW);
            restServiceResult.setMsg("未获取到当前用户信息！");
            return restServiceResult;
        }

        LinkedHashMap detail = lhjcHongHeimdService.selectHongheiMdDetail(jclb, type, recordId);
        boolean isExistFlag = lhjcHongHeimdService.detailIsExistOtherStorage(jclb, type, detail,
                HttpServletHelper.getUserToken(request), "N", recordId);

        if (isExistFlag) {
            restServiceResult.setCode(RestServiceResult.CODE_UNKNOW);
            restServiceResult.setMsg("比对失败");
            return restServiceResult;
        } else {
            lhjcHongHeimdService.modifyJcFlag(jclb, type, "A", recordId);
            restServiceResult.setCode(RestServiceResult.CODE_SUCCESS);
            restServiceResult.setMsg("比对成功");
            return restServiceResult;
        }
    }

    /**
     * 红黑名单奖惩事件发起入库校验
     *
     * @param jclb     奖惩类别：C:惩戒 J:激励
     * @param type     主体类型：C：法人 P:自然人
     * @param recordId 主键id
     * @param request
     * @return
     */
    @RequestMapping(value = "/storeCheck.json")
    public RestServiceResult storeCheck(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb,
                                        @RequestParam(value = "type", defaultValue = "", required = true) String type,
                                        @RequestParam(value = "recordId", defaultValue = "", required = true) String recordId,
                                        HttpServletRequest request) {
        RestServiceResult restServiceResult = new RestServiceResult();
        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;

        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null != userInfo) {

            } else {
                ExceptionLogger.error("LhjcHongHeimdController#storeCheck", "未获取到当前用户信息！");
                restServiceResult.setCode(RestServiceResult.CODE_UNKNOW);
                restServiceResult.setMsg("未获取到当前用户信息！");
                return restServiceResult;
            }
        } else {
            ExceptionLogger.error("LhjcHongHeimdController#storeCheck", "未获取到当前用户信息！");
            restServiceResult.setCode(RestServiceResult.CODE_UNKNOW);
            restServiceResult.setMsg("未获取到当前用户信息！");
            return restServiceResult;
        }

        LinkedHashMap detail = lhjcHongHeimdService.selectHongheiMdDetail(jclb, type, recordId);

        boolean isSuccessFlag = false;

        if (type.equals("C")) {
            isSuccessFlag = lhjcHongHeimdService.addFrLhjcAjbh(jclb, detail, HttpServletHelper.getUserToken(request),
                    "C", recordId, userInfo.getUsername());
        } else {
            isSuccessFlag = lhjcHongHeimdService.addZrrLhjcAjbh(jclb, detail, HttpServletHelper.getUserToken(request),
                    "C", recordId, userInfo.getUsername());
        }

        if (!isSuccessFlag) {
            restServiceResult.setCode(RestServiceResult.CODE_UNKNOW);
            restServiceResult.setMsg("入库失败");
            return restServiceResult;
        } else {
            lhjcHongHeimdService.modifyJcFlag(jclb, type, "A", recordId);
            restServiceResult.setCode(RestServiceResult.CODE_SUCCESS);
            restServiceResult.setMsg("入库成功");
            return restServiceResult;
        }
    }

    /**
     * 联合奖惩自动发起
     *
     * @return
     */
    @RequestMapping(value = "/upLoadLhjcAjfq.do")
    @IgnoreAuthority
    public RestServiceResult<Object> upLoadFileTask() {
        RestServiceResult<Object> result = new RestServiceResult<>();
        try {
            lhjcUploadAjbhShedule.run();
            return result;
        } catch (Exception e) {
            ExceptionLogger.error("lhjcBatchLog#upLoadLhjcFileTask", "上传文件定时任务异常！ERROR：" + e.getMessage(), e);
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("上传附件定时任务异常！");
            return result;
        }
    }

    @RequestMapping(value = "/LhjcHongHeiDetail.json")
    public RestServiceResult<LinkedHashMap> LhjcHongHeiDetail(@RequestParam(value = "jclb", defaultValue = "", required = true) String jclb,
                                                              @RequestParam(value = "type", defaultValue = "", required = true) String type,
                                                              @RequestParam(value = "recordId", defaultValue = "", required = true) String recordId
    ) {
        RestServiceResult<LinkedHashMap> restServiceResult = new RestServiceResult<>();
        try {
            LinkedHashMap map = lhjcHongHeimdService.selectHongheiMdDetail(jclb, type, recordId);
            restServiceResult.setData(map);
            restServiceResult.setCode(RestServiceResult.CODE_SUCCESS);
        } catch (Exception e) {
            ExceptionLogger.error("lhjcBatchLog#LhjcHongHeiDetail", "接口查询红黑名单！ERROR：" + e.getMessage(), e);
            restServiceResult.setCode(RestServiceResult.CODE_UNKNOW);
            restServiceResult.setMsg("红黑名单异常");
        }
        return restServiceResult;
    }

}
